<?php
class Adminqrank_model extends Base_Model
{
    private $_resource_url;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('redis_model');
        $this->load->model('adminform_model');
        $this->load->database();
        $this->_resource_url = $this->config->item('resource_url');
    }

    public function getlist($offset,$limit)
    {
        $start = $offset;
        $end = $start+$limit-1;
        $headers = array('排名','头像','提问者','问题','能量豆','操作');
        $totalnum = $this->redis_model->zcard('question:beansrank');
        $data = $this->redis_model->zrevrange('question:beansrank',$start,$end,true);

        if(!empty($data)){
            $qids = array_keys($data);
            $res = $this->db->select('q.id as qid,q.question,u.userName,u.userIcon')->from('comm_question as q')->join('mod_user as u','u.uid=q.uid','left')->where_in('q.id',$qids)->get()->result_array();

            $format = array();
            foreach ($res as $row){
                $format[$row['qid']]['question']=$row['question'];
                $format[$row['qid']]['userName']=$row['userName'];
                $format[$row['qid']]['userIcon']=preg_match("/http:\/\//i",$row['userIcon'])>0?$row['userIcon']:$this->_resource_url.$row['userIcon'];
            }

            $i = $start+1;
            foreach ($data as $k=>$v) {
                $rank[$i][] = $i;
                $rank[$i][] = '<img src="'.$format[$k]['userIcon'].'" style="height:30px;">';
                $rank[$i][] = $format[$k]['userName'];
                $rank[$i][] = $format[$k]['question'];
                $rank[$i][] = $v;
                $rank[$i][] = $this->adminform_model->getActionList(array(array('controller'=>'admin/commqrank/','function'=>'addanswer','label'=>'回答')),'',$k,0);
                $i++;
            }
            return array('headers'=>$headers,'list'=>array_values($rank),'total'=>$totalnum);
        }else{return array('headers'=>$headers,'list'=>array(),'total'=>$totalnum);}
    }

    public function getResponsedList($offset,$limit)
    {
        $res['headers'] = array('头像','提问者','问题','操作');
        $res['total'] = $this->adminform_model->getPageTotalNum('comm_answer_audio');
        $data = $this->db->select('a.id,q.question,u.userName,u.userIcon')
            ->from('comm_answer_audio as a')
            ->join('comm_question as q','q.id=a.qid','left')
            ->join('mod_user as u','u.uid=q.uid','left')
            ->limit($limit,$offset)
            ->get()->result_array();
        $rowdata = array();
        if(!empty($data)){
            foreach ($data as $row){
                $rowdata['userIcon'] = preg_match("/http:\/\//i",$row['userIcon'])>0?$row['userIcon']:$this->_resource_url.$row['userIcon'];
                $rowdata['userIcon'] = '<img src="'.$rowdata['userIcon'].'" style="height:30px;">';
                $rowdata['userName'] = $row['userName'];
                $rowdata['question'] = $row['question'];
                $rowdata['actionlist'] = $this->adminform_model->getActionList(array(array('controller'=>'admin/commqrank/','function'=>'updateanswer','label'=>'修改回答')),'',$row['id'],0);
                $res['list'][]=$rowdata;
            }
        }else{$res['list']=array();}
        return $res;
    }

    public function setQResponsed($id)
    {
        $answer = $this->db->get_where('comm_answer_audio',array('id'=>$id),1)->row_array();
        if ($answer['qid']>0){
            $data = array();
            $data['isresponsed']=1;
            $data['anslink']='http://wap.nldou.com/comm/audio.html?qid='.$answer['qid'];
            $res = $this->db->update('comm_question',$data,'id='.$answer['qid']);
            if ($res){
                $op = array('type'=>'resq','data'=>array('qid'=>$answer['qid']));
                $op = igbinary_serialize($op);
                $redis = $this->redis_model->getRedisInstance();
                $qbeans = $redis->zScore('question:beansrank',$answer['qid']);
                $redis->multi()->zAdd('question:beansrank:responsed',$qbeans,$answer['qid'])
                    ->zRem('question:beansrank',$answer['qid'])->rPush('notifydeliver',$op)->exec();
            }
        }
        return true;
    }
}